package com.luxsoft.siipap.em.replica.online;

import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.luxsoft.siipap.em.service.EMServiceLocator;

/**
 * Controlador del sistema de replica SIIPAP - SIIPAP-EX
 * 
 * @author Ruben Cancino
 *
 */
public class OnLineImportManager {
	
	private static OnLineImportManager INSTANCE;
	
	private ApplicationContext taskContext;
	
	private OnLineImportManager(){
	}
	
	
	private void init(){
		taskContext=new ClassPathXmlApplicationContext(
				new String[]{"replica-importacion-ctx.xml"}
				,getClass()
				,EMServiceLocator.instance().getContext()
				);
		
	}
	
	public ApplicationContext getTaskContext(){
		return taskContext;
	}
	
	public void addGlobalJobListener(JobListener listener){
		try {
			getScheduler().addGlobalJobListener(listener);
		} catch (SchedulerException e) {
			
		}
	}
	
	protected Scheduler getScheduler(){
		return (Scheduler)taskContext.getBean("replicaScheduler");
	}
	
	public void iniciarReplica() throws SchedulerException{
		getScheduler().start();
	}
	public void pausarReplica() throws SchedulerException{
		getScheduler().standby();
	}
	public void detenerReplica() throws SchedulerException{
		getScheduler().shutdown();
	}
	
	/**
	 * Acceso dinamico  a {@link SchedulerFactoryBean}
	 * 
	 * @return
	 */
	public SchedulerFactoryBean getSchedulerFactoryBean(){
		return (SchedulerFactoryBean)taskContext.getBean("&replicaScheduler");
	}
	
	public  ReplicationManager getReplicationManager(){
		return (ReplicationManager)taskContext.getBean("replicationManager");
	}
	
	
	public static OnLineImportManager getInstance(){
		if(INSTANCE==null){
			INSTANCE=new OnLineImportManager();
			INSTANCE.init();
		}
		return INSTANCE;
	}
	
	public static void main(String[] args) {
		
	}
	
	

}
